package kgc.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import kgc.crm.mapper.BusinessMapper;
import kgc.crm.mapper.ContactsMapper;
import kgc.crm.mapper.ContractMapper;
import kgc.crm.mapper.CustomerMapper;
import kgc.crm.model.Business;
import kgc.crm.model.Contacts;
import kgc.crm.model.Contract;
import kgc.crm.model.Customer;
import kgc.crm.service.IIndexService;
import kgc.crm.util.Constant;
import kgc.crm.util.DateUtil;
import kgc.crm.vo.IndexSalesKitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class IndexServiceImpl implements IIndexService {
    @Autowired
    private CustomerMapper customerMapper; //客户
    @Autowired
    private ContactsMapper contactsMapper;//新增联系人
    @Autowired
    private BusinessMapper businessMapper;//新增商机
    @Autowired
    private ContractMapper  contractMapper;//新增合同
    /**
     *
     * @param timeType 0=当前天，1=本周，2=本月，3=本季度，4=本年份
     * @param type  类型
     * @return
     */
    @Override

    public IndexSalesKitVo salesKit(Integer timeType, Integer type) {
        IndexSalesKitVo vo = new IndexSalesKitVo();
        //查询新增客户
        selectCust(timeType,type,vo);
        //查询新增联系人
        selectCont(timeType,type,vo);
        //查询新增商机表
        selectBusi(timeType,type,vo);
        //查询新增合同
        selectContract(timeType,type,vo);
        //查询新增的合同金额
        selectContractMoney(timeType,type,vo);

        return vo ;
    }

    private void selectContractMoney(Integer timeType, Integer type, IndexSalesKitVo vo) {
        Map<String, Object> map = new HashMap<>();
        String startTime = DateUtil.newDateByTimeType(timeType);
        String  moeny =  "" ;
        QueryWrapper<Contract> wrapper = new QueryWrapper<Contract>();
        wrapper.ge(Customer.COL_CREATE_TIME,startTime)
                .ge(Contract.COL_MONEY,moeny)
                .le(Customer.COL_CREATE_TIME,DateUtil.format(
                        new Date(), Constant.DATE_TIME_YYYY_MM_DD_HH_MM_SS
                ));
        vo.setContractCount(contractMapper.selectCount(wrapper));
    }

    private void selectContract(Integer timeType, Integer type, IndexSalesKitVo vo) {
        Map<String, Object> map = new HashMap<>();
        String startTime = DateUtil.newDateByTimeType(timeType);
        QueryWrapper<Contract> wrapper = new QueryWrapper<Contract>();
        wrapper.ge(Customer.COL_CREATE_TIME,startTime)
                .le(Customer.COL_CREATE_TIME,DateUtil.format(
                        new Date(), Constant.DATE_TIME_YYYY_MM_DD_HH_MM_SS
                ));
        vo.setContractCount(contractMapper.selectCount(wrapper));
    }

    private void selectBusi(Integer timeType, Integer type, IndexSalesKitVo vo) {
        Map<String, Object> map = new HashMap<>();
        String startTime = DateUtil.newDateByTimeType(timeType);
        QueryWrapper<Business> wrapper = new QueryWrapper<Business>();
        wrapper.ge(Customer.COL_CREATE_TIME,startTime)
                .le(Customer.COL_CREATE_TIME,DateUtil.format(
                        new Date(), Constant.DATE_TIME_YYYY_MM_DD_HH_MM_SS
                ));
        vo.setBusiCount(businessMapper.selectCount(wrapper));
    }

    private void selectCont(Integer timeType, Integer type, IndexSalesKitVo vo) {
        Map<String, Object> map = new HashMap<>();
        String startTime = DateUtil.newDateByTimeType(timeType);
        QueryWrapper<Contacts> wrapper = new QueryWrapper<Contacts>();
        wrapper.ge(Customer.COL_CREATE_TIME,startTime)
                .le(Customer.COL_CREATE_TIME,DateUtil.format(
                        new Date(), Constant.DATE_TIME_YYYY_MM_DD_HH_MM_SS
                ));
        vo.setContCount(contactsMapper.selectCount(wrapper));
    }

    private void selectCust(Integer timeType, Integer type, IndexSalesKitVo vo) {
        Map<String, Object> map = new HashMap<>();
        String startTime = DateUtil.newDateByTimeType(timeType);
        QueryWrapper<Customer> wrapper = new QueryWrapper<Customer>();
        wrapper.ge(Customer.COL_CREATE_TIME,startTime)
                .le(Customer.COL_CREATE_TIME,DateUtil.format(
                        new Date(), Constant.DATE_TIME_YYYY_MM_DD_HH_MM_SS
                ));
        vo.setCustCount(customerMapper.selectCount(wrapper));
    }

}
